#include <Misc.au3>
#include <ScreenCapture.au3>
#include <..\utility.au3>

HotKeySet("{PAUSE}", "theExit")
HotKeySet("9", "theExit")

call("screenshot", "start")

$numsweeps = IniRead("settings.ini", "run", "numsweeps", 1)

$stormarmor = 1 ;storm armor is slot 1
$explosion = 1 ;archon explosion is slot 1
$teleport = 2 ;teleport is slot 2
$magicweapon = 3 ;magic weapon is slot 3
$familiar = 4 ;familiar is slot 4
$goldpiles = 5 ;Number of gold piles to pick up

$alchnum = 0 ;Number of times alchemist spawned


While 1 
	processkey('70', "mousePos") ;F1
	processkey('71', "leorics") ;F2
	processkey('72', "getLegendaries") ;F3
	processkey('73', "repair") ;F4
	processkey('74', "findring") ;F5
	processkey('75', "findcolor") ;F6
	processkey('76', "checkdead") ;F7
	processkey('77', "castSpelltest") ;F8
	processkey('78', "checkdisco") ;F9
	processkey('79', "getGems") ;F10
WEnd

Func leorics()	
While 1
	Sleep($lagfactor)
	
	;Log Run Start
	call("logmessage", "Starting Run " & $runs)
	$runs = $runs + 1
	
	;Randomly take a break
	If $runs > 10 Then
		call("takebreak")
	EndIf
	
	;Repair if needed
	call("repair")
	
	;Move to portal
	call("fuzzyclick","left", 113, 24)
	
	call("fuzzysleep",4600 * $runspeed)
	Sleep($lagfactor)
	
	;teleport up cliff
	call("fuzzymove", 1767,0)
	Sleep(Random(100,500,1))
	Send("2")
	Sleep(Random(400,600,1))
	
	;Move Right
	Send("{CTRLDOWN}")
	call("fuzzyclick", "left", 1777, 634)
	call("fuzzysleep", Random(2400,2600,1))
	
	;Move Right
	call("fuzzyclick", "left", 1680, 1072)
	call("fuzzysleep", Random(2300,2600,1))
	Send("{CTRLUP}")
	
	;Check for alch????
	If AlchLives()=True Then
		$alchnum = $alchnum + 1
		call("logmessage", "Found Alchemist " & $alchnum)
		
		call("fuzzyclick", "left", 1128, 934)
		call("fuzzysleep", Random(1500,2000,1))
		
		;Close all windows, send - a few times
		For $i = 0 to Random(4,15,1) Step 1
			Send("{NUMPADSUB}")
			Sleep(Random(200,300,1))
		Next
	
		;Move Back
		Send("{CTRLDOWN}")
		call("fuzzyclick", "left", 461, 0)
		call("fuzzysleep", Random(150,175,1))
		call("fuzzyclick", "left", 461, 0)
		call("fuzzysleep", Random(4200,4400,1))
		Send("{CTRLUP}")
		
		;164,522
		call("fuzzymove",164,522)
		Send("2")
		Sleep(Random(300,500))
	
		;Move Back
		;call("fuzzyclick", "left", 131, 540)
		;call("fuzzysleep", Random(3000,3200,1))
		
	
		;teleport down cliff
		call("fuzzymove", 300, 1079)
		Sleep(Random(100,500,1))
		Send("3")
		Sleep(Random(400,600,1))
		Send("3")
		Sleep(Random(400,600,1))
		
		
		$killswitch = 0
		For $i = 960 to 1310 Step 10
			If $killswitch = 20 Then
				call("logmessage", "killswitch hit")
				call("screenshot", "killswitch")
				ExitLoop
			EndIf
			
			;Move mouse to general hermit area
			call("fuzzymove", $i, 390)
		
			;check for white text??
			If MouseOnHermit($i) = 1 Then
				Sleep(100)
				MouseClick("left")
				Sleep(100)
				MouseClick("left")
				ExitLoop
			EndIf
			
			If $i = 1300 Then
				$i = 960
				$killswitch = $killswitch + 1
			EndIf
		Next
		
		;Close all windows, send - a few times
		For $i = 0 to Random(4,9,1) Step 1
			Send("{NUMPADSUB}")
			Sleep(Random(200,300,1))
		Next
	Else
		MouseClick("left", Random(699,1265), Random(278,702))
		Sleep(Random(300,1000,1))
	EndIf

	call("exitgame")
	
	;Restart Script
WEnd	
	
EndFunc

Func AlchLives()
	$alchcolor = 0x873A0B
	
	$coord = PixelSearch(1120,844,1168,911,$alchcolor,5)
	If IsArray($coord) Then
		Return 1
	EndIf
	
	Return 0
EndFunc

Func MouseOnHermit($i)
	$hermitcolor = 0xFFFFFF
	
	$coord = PixelSearch($i-70,281,$i+70,380,$hermitcolor)
	If IsArray($coord) Then
		Return 1
	EndIf
	
	Return 0
EndFunc